local jass = require 'jass.common'
local dbg  = require 'jass.debug'

-- 注意:游戏初始化阶段显示无效 --

local dialog = {}
setmetatable(dialog,dialog)
ac.dialog = dialog

-- 存放所有对话框
dialog.alldialogs = {}
dialog.allbuttons = {}

-- 创建对话框
function dialog:__call(title)
    local handle = jass.DialogCreate()
    dbg.handle_ref(handle)
    jass.TriggerRegisterDialogEvent(dialog._handle,handle)
    local dlg = {}
    dlg.title = title
    dlg.handle = handle 
    setmetatable(dlg,dialog)
    dlg:init()
    dialog.alldialogs[handle] = dlg
    return dlg
end

-- 输出
function dialog:__tostring()
	return '对话框'
end

-- 模块初始化
function dialog.init()
    local handle = jass.CreateTrigger()
    dbg.handle_ref(handle)  
    jass.TriggerAddAction(handle,dialog.action)
    dialog._handle = handle
end 

--获取对话框
function dialog.j_dialog(handle)
    return dialog.alldialogs[handle]
end 

--获取对话框按钮
function dialog.j_button(handle)
    return dialog.allbuttons[handle]
end 

-- 触发器回调
function dialog.action()
    local dlg = dialog.j_dialog(jass.GetClickedDialog())
    local btn = dialog.j_button(jass.GetClickedButton())
    local ply = ac.player(jass.GetTriggerPlayer())
    ply:notify('玩家-点击对话框', ply, dlg, btn)
    if dlg.on_click then 
        dlg:on_click(btn, ply)
    end 
end 

local mt = {}
dialog.__index = mt 

-- 结构
mt.type = 'dialog'

-- 标题
mt.title = '对话框'

--句柄
mt.handle = jass.ConvertUnitState(0)

-- 初始化
function mt:init()
    self._buttons = {}
    if package.local_test then 
        --- 对不是底层创建的对象记录路径 
        --- 用来热更新删除
        local info = debug.getinfo(3, "Sl") or {}
        local bool = info.short_src:find('core')
        if not bool then 
            self._src = info.short_src
            self._line = info.currentline
        end 
    end
end

-- 设置标题
function mt:set_title(title)
    self.title = title 
    jass.DialogSetMessage(self.handle,self.title)
end

-- 添加按钮
function mt:add_button(button)
    if not button or not button.key then return end;
    
    local key = button.key
    local name = button.name
    local hotkey = button.hotkey
    local code = 0

    if not name then
        name = key
    end 

    local title = name  

    if hotkey then 
        title = title .. (' (%s) '):format(hotkey)
        code = ac.message.keyboard[string.upper(hotkey)] or 0
    end

    local handle = jass.DialogAddButton(self.handle, title, code)
    if handle == 0 then 
        return 
    end 

    dbg.handle_ref(handle)
    button.name = name
    button.handle = handle

    table.insert(self._buttons, button)
    dialog.allbuttons[handle] = button
    return button
end

-- 清空
function mt:clear()
    for i,handle in ipairs(self._buttons) do 
        dbg.handle_unref(handle)
        dialog.allbuttons[handle] = nil 
    end
    self.on_click = nil
    self._buttons = {}
    jass.DialogClear(self.handle)
end

-- 删除
function mt:remove()
    if self.removed then
        return
    end 
    self.removed = true
    jass.DialogDisplay(jass.GetLocalPlayer(),self.handle,false)
    jass.DialogClear(self.handle)
    jass.DialogDestroy(self.handle)
    dbg.handle_unref(self.handle)
    dialog.alldialogs[self.handle] = nil 
    self.handle = nil 
end

--显示
function mt:show(ply)
    jass.DialogSetMessage(self.handle,self.title)
    if ply then 
        jass.DialogDisplay(ply.handle,self.handle,true)
    else
        jass.DialogDisplay(jass.GetLocalPlayer(),self.handle,true)
    end
end

--隐藏
function mt:hide(ply)
    if ply then 
        jass.DialogDisplay(ply.handle,self.handle,false)
    else
        jass.DialogDisplay(jass.GetLocalPlayer(),self.handle,false)
    end
end

return dialog 